//
//  SHKRenren.m
//  YueMi
//
//  Created by Rainbow on 5/19/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "SHKRenren.h"
#import "SHKRenrenForm.h"


@implementation SHKRenren
@synthesize xAuth;
@synthesize delegate;
@synthesize renren;
- (id)init{
	if ((self = [super init])){	
        
        renren = [[Renren alloc] initWithAppKeyAndId:SHKRenrenKey andAppId:SHKRenrenAppId];
        

	}	
	return self;
}


#pragma mark -
#pragma mark Configuration : Service Defination

+ (NSString *)sharerTitle{
	return @"人人分享";
}

+ (BOOL)canShareURL{
	return YES;
}

+ (BOOL)canShareText{
	return YES;
}

// TODO use img.ly to support this
//+ (BOOL)canShareImage
//{
//	return YES;
//}


#pragma mark -
#pragma mark Configuration : Dynamic Enable

- (BOOL)shouldAutoShare{
	return NO;
}


#pragma mark -
#pragma mark Authorization

- (BOOL)isAuthorized{		
	return [renren isSessionValid];
}

- (void)promptAuthorization{		
    [renren authorize:[NSArray arrayWithObjects:@"publish_feed",@"photo_upload", nil] delegate:self];
}

-(void)rrDidLogin
{
    [self show];
}

-(void)rrDidLogout
{
    
}

- (void)show{
    [self showRenrenForm];
}

- (void)showRenrenForm{
	SHKRenrenForm *rootView = [[SHKRenrenForm alloc] initWithNibName:nil bundle:nil];	
	rootView.delegate = self;
	
	// force view to load so we can set textView text
	[rootView view];
	
	rootView.textView.text = self.item.text;
	rootView.hasAttachment = self.item.image != nil;
	
	[self pushViewController:rootView animated:NO];
	
	[[SHK currentHelper] showViewController:self];	
}

#pragma mark -

- (BOOL)send{	
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:0];
//    [params setObject:@"users.getInfo" forKey:@"method"];
    [params setObject:@"share.publish" forKey:@"method"];
    [params setObject:@"10" forKey:@"type"];
    [params setObject:@"JSON" forKey:@"format"];
    [params setObject:@"http://v.youku.com/v_show/id_XMjU4MzQ4MTAw.html" forKey:@"url"];
    [renren requestWithParams:params andDelegate:self];
    [params release];
    [self sendDidStart];
    return YES;
    
}

- (void)sendForm:(SHKRenrenForm *)form{	
	[self.item setCustomValue:form.textView.text forKey:@"status"];
	[self tryToSend];
}

-(void)request:(Request *)request didReceiveResponse:(NSURLResponse *)response
{
    
}

-(void)request:(Request *)request didLoad:(id)result
{
    SBJSON *parser = [[[SBJSON alloc] init] autorelease];
    NSString *response = [parser stringWithObject:result];
    NSLog(@"response: %@", response);
    [self sendDidFinish];
}

-(void)request:(Request *)request didFailWithError:(NSError *)error
{
    [self sendDidFailWithError:nil];
}

-(void)dealloc
{
    [renren release];
    [super dealloc];
}

@end
